
package id.isran.kamusindonesiamuna.fragment;

import id.isran.kamusindonesiamuna.R;
import id.isran.kamusindonesiamuna.basisdata.Data;
import id.isran.kamusindonesiamuna.utils.Utils;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentKataInaMuna extends Fragment implements OnClickListener {

    private Button btn_terjemahkan;
    private AutoCompleteTextView autoCompleteText;
    private TextView txt_kata_dicari;
    private TextView txt_kata_terjemahan;
    private ArrayAdapter<String> autoCompleteAdapter;

    // array variable untuk fitur auto complete
    private String[] dataAutoComplete;
    private ImageView btn_play;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.TRACE("FragmentKataInaMuna", "onCreate: ");

        loadData();

        autoCompleteAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.single_item_dropdown_autocomplete, dataAutoComplete);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Utils.TRACE("FragmentKataInaMuna", "onAttach :run ");
    }

    /**
     * load data ke array untuk fitur autocomplete
     */
    private void loadData() {
        dataAutoComplete = new String[Data.BARIS_COUNT_INA_MUNA + 1];
        for (int i = 0; i < Data.BARIS_COUNT_INA_MUNA + 1; i++) {
            dataAutoComplete[i] = Data.dataInaMuna[i][Data.INDEX_KATA];
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Utils.TRACE("FragmentKataInaMuna", "onCreateView: ");
        ViewGroup view = (ViewGroup) inflater.inflate(
                R.layout.fragment_kata_ina, null);
        autoCompleteText = (AutoCompleteTextView) view.findViewById(R.id.et_search);
        txt_kata_dicari = (TextView) view.findViewById(R.id.txt_kata_dicari);
        txt_kata_terjemahan = (TextView) view.findViewById(R.id.txt_kata_terjemahan);
        btn_terjemahkan = (Button) view.findViewById(R.id.btn_terjemahkan);
        btn_play = (ImageView) view.findViewById(R.id.btnPlayKataIna);

        btn_terjemahkan.setOnClickListener(this);
        btn_play.setOnClickListener(this);
        autoCompleteText.setAdapter(autoCompleteAdapter);

        return view;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btn_terjemahkan) {
            // cek apakah automcomplete kosong
            if (!TextUtils.isEmpty(autoCompleteText.getText().toString())) {
                txt_kata_dicari.setText("" + autoCompleteText.getText().toString());
                txt_kata_terjemahan.setText(""
                        + Data.cariKata(autoCompleteText.getText().toString().toLowerCase(),
                                Data.INDO_MUNA));
            } else {
                Toast.makeText(getActivity(), "Isikan kata yang ingin dicari", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (v == btn_play) {
            if (!TextUtils.isEmpty(autoCompleteText.getText().toString())) {
                Utils.playAudio(getActivity(),
                        Utils.SOUND_INA + "" + autoCompleteText.getText());
            }
        }

    }
}
